package com.channel.temperature.collect.ui.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.channel.temperature.collect.app.dao.OfflineBase
import com.channel.temperature.collect.data.response.CachedDeviceBean
import kotlinx.coroutines.launch
import me.hgj.mvvmhelper.base.BaseViewModel
import me.hgj.mvvmhelper.base.appContext

class CachedDeviceListModel : BaseViewModel() {
    var listData = MutableLiveData<ArrayList<CachedDeviceBean>>()
    var deviceList = ArrayList<String>()
    fun getCachedDataList() {
        viewModelScope.launch {
            deviceList = OfflineBase.get(
                appContext
            ).offlineDao().queryRoomDeviceNameList() as ArrayList<String>
            Log.e("getCachedDataList", "getCachedDataList " + deviceList?.size)
            if (deviceList.isNullOrEmpty()) {
                listData.value = ArrayList<CachedDeviceBean>()
            } else {
                deviceList.sortByDescending { it.split("&").get(1).toLong() }
                var tempList = ArrayList<CachedDeviceBean>()
                for (i in 0 until deviceList.size) {
                    var totalCount = OfflineBase.get(
                        appContext
                    ).offlineDao().queryRoomCountByDeviceName(deviceList.get(i))
                    tempList.add(CachedDeviceBean(deviceList[i], totalCount))
                }
                tempList.sortByDescending { it.name.split("&").get(1).toLong() }
                listData.value = tempList

            }
        }
    }

}